package medM;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import cureClassify.CureClassifyBean;
import dbTools.DBTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Servlet implementation class MedMessageUpdateServlet
 */
@WebServlet("/MedMessageUpdateServlet")
public class MedMessageUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MedMessageUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		int id=Integer.parseInt(request.getParameter("id"));
		DBTools db=new DBTools();
		db.open();
		String sql="SELECT * FROM medInfo WHERE id="+id;
		ResultSet rs=db.select(sql);
		ArrayList al=new ArrayList();
		try {
			while(rs.next()){
				MedMessageBean mmb=new MedMessageBean(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),rs.getString(11),rs.getString(12),rs.getString(13),rs.getString(14),rs.getString(15),rs.getInt(16),rs.getString(17),rs.getString(18),rs.getString(19),rs.getString(20),rs.getString(21),rs.getString(22),rs.getString(23),rs.getString(24),rs.getString(25));
				al.add(mmb);
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String sqls="SELECT * FROM cureTypeInfo";
		ResultSet rss=db.select(sqls);
		ArrayList als=new ArrayList();
		try {
			while(rss.next()){
				CureClassifyBean ccb=new CureClassifyBean(rss.getInt(1),rss.getString(2),rss.getString(3));
				als.add(ccb);
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		db.close();
		MedMessageBean mmb=(MedMessageBean) al.get(0);
		request.setAttribute("mmb", mmb);
		request.setAttribute("al", als);
		request.getRequestDispatcher("/MedMessageUpdate.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		String photo=null;
		SmartUpload su=new SmartUpload();
		PageContext page=JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 1024, true);
		su.initialize(page);
		try {
			su.upload();
			su.save("D:\\19����������\\�ڶ�ѧ��\\JAVAWEB�߼��������\\JAVAWEB�߼��������(ʵ��)\\xbSystems\\WebContent\\img\\goodsPhotos");
			Files fs=su.getFiles();
			File f=fs.getFile(0);
			photo=f.getFileName();
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int id=Integer.parseInt((String) su.getRequest().getParameter("id"));
		String normalName=(String) su.getRequest().getParameter("normalName");
		String medName=(String) su.getRequest().getParameter("medName");
		String englishName=(String) su.getRequest().getParameter("englishName");
		String company=(String) su.getRequest().getParameter("company");
		String symptom=(String) su.getRequest().getParameter("symptom");
		String dosage=(String) su.getRequest().getParameter("dosage");
		String taboo=(String) su.getRequest().getParameter("taboo");
		String matters=(String) su.getRequest().getParameter("matters");
		String untoward=(String) su.getRequest().getParameter("untoward");
		String drug=(String) su.getRequest().getParameter("drug");
		String element=(String) su.getRequest().getParameter("element");
		String number=(String) su.getRequest().getParameter("number");
		String oa=(String) su.getRequest().getParameter("oa");
		String research=(String) su.getRequest().getParameter("research");
		int classification=Integer.parseInt((String) su.getRequest().getParameter("classification"));
		String dose=(String) su.getRequest().getParameter("dose");
		String mins=(String) su.getRequest().getParameter("mins");
		String maxs=(String) su.getRequest().getParameter("maxs");
		String usedose=(String) su.getRequest().getParameter("usedose");
		String packing=(String) su.getRequest().getParameter("packing");
		String packingdose=(String) su.getRequest().getParameter("packingdose");
		String times=(String) su.getRequest().getParameter("times");
		String intervnal=(String) su.getRequest().getParameter("intervnal");
		int i=1;
		int j=0;
		DBTools db = new DBTools();
		db.open();
		String sqls="UPDATE medInfo SET normalName='"+normalName+"',medName='"+medName+"',englishName='"+englishName+"',company='"+company+"',symptom='"+symptom+"',dosage='"+dosage+"',taboo='"+taboo+"',matters='"+matters+"',untoward='"+untoward+"',drug='"+drug+"',element='"+element+"',number='"+number+"',oa='"+oa+"',research='"+research+"',classification="+classification+",dose='"+dose+"',mins='"+mins+"',maxs='"+maxs+"',usedose='"+usedose+"',packing='"+packing+"',packingdose='"+packingdose+"',times='"+times+"',intervnal='"+intervnal+"',photos='"+photo+"' WHERE id="+id;
		db.update(sqls);
		String sql="SELECT * FROM medInfo";
		ResultSet rs=db.select(sql);
		ArrayList al=new ArrayList();
		try {
			while(rs.next()){
				MedMessageBean mmb=new MedMessageBean(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),rs.getString(11),rs.getString(12),rs.getString(13),rs.getString(14),rs.getString(15),rs.getInt(16),rs.getString(17),rs.getString(18),rs.getString(19),rs.getString(20),rs.getString(21),rs.getString(22),rs.getString(23),rs.getString(24),rs.getString(25));
				al.add(mmb);
			}
			if(al.size()%5!=0){
				j=al.size()/5+1;
				if(i<1){
					i=al.size()/5+1;
				}
				if(i>al.size()/5+1){
					i=1;
				}
			}else{
				j=al.size()/5;
				
				if(i<1){
					i=al.size()/5;
				}
				if(i>al.size()/5){
					i=1;
				}
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String sql2="select * from medInfo ORDER BY id DESC limit "+(i-1)*5+","+"5";
		ResultSet rss=db.select(sql2);
		ArrayList al1=new ArrayList();
		try {
			while(rss.next()){
				MedMessageBean mmb=new MedMessageBean(rss.getInt(1),rss.getString(2),rss.getString(3),rss.getString(4),rss.getString(5),rss.getString(6),rss.getString(7),rss.getString(8),rss.getString(9),rss.getString(10),rss.getString(11),rss.getString(12),rss.getString(13),rss.getString(14),rss.getString(15),rss.getInt(16),rss.getString(17),rss.getString(18),rss.getString(19),rss.getString(20),rss.getString(21),rss.getString(22),rss.getString(23),rss.getString(24),rss.getString(25));
				al1.add(mmb);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.close();
		request.setAttribute("al", al1);
		request.setAttribute("i", i);
		request.setAttribute("j", j);
		request.getRequestDispatcher("/medMessage.jsp").forward(request, response);
	}

}
